/**
 * Copyright © 2011 looty - Textoo
 * @link http://www.textoo.org
 * @link http://code.google.com/p/textoo/
 * @author lab9 - Bertrand Larrieu
 * @mail lab9.fr@gmail.com
 * @version 2.0
 */

package org.looty.core.textoo.context.style 
{
	import flash.events.*;
	import flash.text.*;
	import flash.utils.*;
	import org.looty.log.*;
	import org.looty.textoo.events.*;
	
	
	public class StyleManager extends EventDispatcher
	{
		
		private var _styleSheets		:Vector.<Dictionary>;
		private var _styleSheet			:StyleSheet;
		private var _title				:String;
		private var _index				:int;
		
		static private const PREFERRED	:String = "preferred";
		static private const CHANGE		:TextooEvent = new TextooEvent(TextooEvent.STYLESHEET_CHANGE);
		
		public function StyleManager() 
		{
			_styleSheets = new Vector.<Dictionary>();
			_title = PREFERRED;
			_styleSheet = null;
			_index = -1;
		}
		
		public function addStyleSheet(CSSText:String, index:uint, title:String = ""):void
		{
			if (CSSText.length == 0) return;
			if (title == "") title = PREFERRED;
			
			if (_styleSheets[index] == null) _styleSheets[index] = new Dictionary();		
			
			var selected:TextooStyleSheet = _styleSheets[index][title];
			if (selected == null)
			{
				selected = new TextooStyleSheet(title);
				_styleSheets[index][title] = selected;
			}
			
			selected.parseCSS(CSSText);
			
			if (selected != styleSheet) update();
			else dispatchEvent(CHANGE);
		}
		
		public function setLength(length:uint):void 
		{
			_styleSheets.fixed = false;
			_styleSheets.length = length;
			_styleSheets.fixed = true;
		}
		
		private function getStyleSheet(index:uint, title:String):StyleSheet
		{		
			for each (var styleSheet:TextooStyleSheet in _styleSheets[_index]) if (styleSheet.title == title) return styleSheet;			
			return null;
		}
		
		private function update():void 
		{				
			switch(true)
			{
				case _index == -1:
				case _styleSheets[_index] == null:
					//setStyleSheet(null);
				break;
				
				case _styleSheets[_index][_title] is TextooStyleSheet :
					setStyleSheet(_styleSheets[_index][_title]);
					Looger.info("switch to styleSheet - title:" + _title);
				break;
				
				case _styleSheets[_index][PREFERRED] is TextooStyleSheet :
					setStyleSheet(_styleSheets[_index][PREFERRED]);
					Looger.warn("missing styleSheet - title : '" + _title + "' - fall back to preferred");
				break;
				
				case getFirstTitle() is TextooStyleSheet :
					setStyleSheet(getFirstTitle());
					Looger.warn("missing styleSheet - title : '" + _title + "' - fall back to first in list");
				break;
				
				default:
				Looger.error("missing styleSheet - title : '" + _title + "'");
				
			}		
		}
		
		private function getFirstTitle():TextooStyleSheet
		{
			for each (var styleSheet:TextooStyleSheet in _styleSheets[_index]) return styleSheet;
			return null;
		}
		
		private function setStyleSheet(value:TextooStyleSheet):void
		{
			if (_styleSheet == value) return;
			_styleSheet = value;
			dispatchEvent(CHANGE);
		}
		
		public function get styleSheet():StyleSheet 
		{
			return _styleSheet;
		}
		
		public function get title():String { return _title; }
		
		public function set title(value:String):void 
		{
			_title = value;
			update();
		}		
		
		public function get index():int 
		{
			return _index;
		}
		
		public function set index(value:int):void 
		{
			_index = value;
			update();
		}
		
		public function dispose():void
		{
			
		}
		
		public function dump():void 
		{
			Looger.info(" -- stylesheets --");
			for (var i:int = 0; i < _styleSheets.length; i++) for each (var styleSheet:TextooStyleSheet in _styleSheets[i]) Looger.info(" [" + i + "] " + styleSheet.title);
		}		
		
	}

}